import { ApiProperty } from '@nestjs/swagger';
import { HttpStatus } from '@nestjs/common';

export class ApiResponseDto<T> {
  @ApiProperty({ description: '状态码' })
  code: number;

  @ApiProperty({ description: '消息' })
  message: string;

  @ApiProperty({ description: '数据', required: false })
  data?: T;

  constructor(code: number, message: string, data?: T) {
    this.code = code;
    this.message = message;
    this.data = data;
  }

  static success<T>(data?: T, message: string = '操作成功'): ApiResponseDto<T> {
    return new ApiResponseDto<T>(HttpStatus.OK, message, data);
  }

  static error(message: string = '操作失败', code: number = HttpStatus.BAD_REQUEST): ApiResponseDto<null> {
    return new ApiResponseDto<null>(code, message, null);
  }

  static created<T>(data?: T, message: string = '创建成功'): ApiResponseDto<T> {
    return new ApiResponseDto<T>(HttpStatus.CREATED, message, data);
  }

  static notFound(message: string = '资源不存在'): ApiResponseDto<null> {
    return new ApiResponseDto<null>(HttpStatus.NOT_FOUND, message, null);
  }

  static forbidden(message: string = '没有权限'): ApiResponseDto<null> {
    return new ApiResponseDto<null>(HttpStatus.FORBIDDEN, message, null);
  }

  static unauthorized(message: string = '未授权'): ApiResponseDto<null> {
    return new ApiResponseDto<null>(HttpStatus.UNAUTHORIZED, message, null);
  }
} 